/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.global;

import cn.ac.iie.di.datadock.rdata.exchange.client.v1.REMessage;
import cn.ac.iie.di.datadock.rdata.exchange.client.v1.connection.REConnection;
import cn.ac.iie.di.httprequest.HttpMessage;
import cn.ac.iie.di.httprequest.MyHttpRequest1;
import cn.ac.iie.di.msgobject.Message;
import cn.ac.iie.di.redisutil.RedisUtil;
import cn.ac.iie.di.rocketmq.RocketMqClient;
import cn.ac.iie.di.rocketmq.RocketMqProducer;
import cn.ac.iie.di.rocketmq.SendToAbandon;
import cn.ac.iie.di.util.ConfigureUtil;
import cn.ac.iie.di.worker.DownPicAction;
import cn.ac.iie.di.worker.DwdPic;
import com.alibaba.fastjson.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.log4j.Logger;

/**
 *
 * @author spf
 */
public class GlobalParas {

    public static Logger log = Logger.getLogger(GlobalParas.class.getName());
    public static ConfigureUtil cfg = null;

    //控制每天访问腾讯接口的次数
    public static String dateInfo = "";
    public static AtomicInteger countQuest = new AtomicInteger(0);
    public static int questMax = 0;

    public static String localhostName;
    //mq
    public static String nameServer = "";
    public static String redishosts = "";
    public static String redismastername = "";
    public static int redisdbname;
    public static int redisTimeout;
    public static RedisUtil redisUtil;
    public static ArrayBlockingQueue<REMessage> oriMsgPool = null;
    public static ArrayBlockingQueue<HashMap> dwdMsgPool = null;
    public static ArrayBlockingQueue<HashMap> sendMsgPool = null;
    public static ArrayBlockingQueue<HashMap> sendMsgPool2 = null;
    public static int maxTry;
    public static int maxConnTimeLimit;
    public static int maxReadTimeLimit;
    public static int bagSize;
    public static REConnection conn = null;
    public static String[] sendMq = null;
    public static String sendGmsgMq = "";
    public static String urlColumnName = "";
    public static String pathColumnName = "";
    public static String newColumn = "";

    public static String midColumnName = "";
    public static String midColumnString = "";

    public static String httpUrl = "";
    public static String[] oriMsgMq = null;
    public static String rootPath = "";
    public static long waittingTime;

    public static int parellNumGet;
    public static int parellNumPae;
    public static int parellNumDwd;
    public static int parellNumDwdA;
    public static int parellNumSend;
    public static int divideNum;

    //killHandler
//        public static boolean isShouldExit = false;
    public static boolean hasBeenExited = false;
    public static boolean killMonitor = false;
    public static boolean killRemsg2Msg = false;
    public static boolean killDownPic = false;
    public static boolean killDPAction = false;
    public static boolean killsendMsg = false;

    public static int isTwb;
    public static int isIcon;

    public static Map<String, String> mpForMqName = new HashMap<>();
    public static Map<String, ArrayBlockingQueue<Message>> mpForSendPool = new HashMap();

    //每创建一个线程之后，都会将这个线程放入到一个blockingqueue中，然后，这个记录可以用来后期关闭程序
    public static ArrayBlockingQueue<RocketMqClient> pullMsgQueue = new ArrayBlockingQueue<>(100);
    public static ArrayBlockingQueue<Message> ReMsg2MsgQueue = new ArrayBlockingQueue<>(100);
    public static ArrayBlockingQueue<DwdPic> DwdPicQueue = new ArrayBlockingQueue<>(100);
    public static ArrayBlockingQueue<DownPicAction> DwdPicAQueue = new ArrayBlockingQueue<>(200);
    public static ArrayBlockingQueue<RocketMqProducer> MqProducerQueue = new ArrayBlockingQueue<>(100);
    public static ArrayBlockingQueue<SendToAbandon> MqSendQueue = new ArrayBlockingQueue<>(100);

    //pic sendback
    public static ArrayBlockingQueue<List> urlAndAddrPool = null;
    //20171012添加，發送到http請求的那邊的
    public static ArrayBlockingQueue<HashMap> myRequestPool = null;
    public static ArrayBlockingQueue<String> send2MyHttpRequestPool = null;
    public static boolean killHttpMsg = false;
    public static boolean killHttpRequest = false;
    public static ArrayBlockingQueue<HttpMessage> httpMsgQueue = new ArrayBlockingQueue<>(100);
    public static String[] httpPostAddr;
    public static ArrayBlockingQueue<JSONObject> send2RabbitmqPool = null;
    public static ArrayBlockingQueue<MyHttpRequest1> httpRequestPool = new ArrayBlockingQueue<>(100);
    public static int httpTheadSize;

    public static void init() {
        try {
            //获取配置文件
            cfg = new ConfigureUtil(ClassLoader.getSystemClassLoader().getResource("config.properties").getFile());
        } catch (Exception e) {
            log.error(e + ",the program will exit", e);
            return;
        }

        GlobalParas.questMax = cfg.getIntProperty("questMax");
        Calendar c = Calendar.getInstance();
        GlobalParas.dateInfo = c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.DATE);
        log.info("dateinfo is : " + GlobalParas.dateInfo);

        GlobalParas.localhostName = cfg.getProperty("localhostName");

        GlobalParas.isTwb = Integer.parseInt(cfg.getProperty("isTwb"));
        GlobalParas.isIcon = cfg.getIntProperty("isIcon");
        GlobalParas.bagSize = Integer.parseInt(cfg.getProperty("bagSize"));
        GlobalParas.waittingTime = Long.parseLong(cfg.getProperty("waittingTime"));

        GlobalParas.parellNumGet = Integer.parseInt(cfg.getProperty("parellNumGet"));
        GlobalParas.parellNumPae = Integer.parseInt(cfg.getProperty("parellNumPae"));
        GlobalParas.parellNumDwd = Integer.parseInt(cfg.getProperty("parellNumDwd"));
        GlobalParas.parellNumDwdA = cfg.getIntProperty("parellNumDwdA");
        GlobalParas.parellNumSend = Integer.parseInt(cfg.getProperty("parellNumSend"));
        GlobalParas.divideNum = Integer.parseInt(cfg.getProperty("divideNum"));

        //get MQ's nameServer and topic and groupName
        GlobalParas.oriMsgMq = cfg.getProperty("oriMsgMq").split("[|]");
        GlobalParas.nameServer = cfg.getProperty("nameServer");
        //set redis
        GlobalParas.redisTimeout = Integer.parseInt(cfg.getProperty("redisTimeout"));
        GlobalParas.redishosts = cfg.getProperty("redishosts");
        GlobalParas.redismastername = cfg.getProperty("redismastername");
        GlobalParas.redisdbname = Integer.valueOf(cfg.getProperty("redisdbname"));
        GlobalParas.redisUtil = new RedisUtil(GlobalParas.redishosts, GlobalParas.redismastername, GlobalParas.redisdbname);

        GlobalParas.conn = new REConnection(GlobalParas.nameServer);
        GlobalParas.oriMsgPool = new ArrayBlockingQueue(cfg.getIntProperty("oriMsgPool"));
        GlobalParas.dwdMsgPool = new ArrayBlockingQueue(cfg.getIntProperty("dwdMsgPool"));
        GlobalParas.sendMsgPool = new ArrayBlockingQueue(cfg.getIntProperty("sendMsgPool"));
        GlobalParas.sendMsgPool2 = new ArrayBlockingQueue(cfg.getIntProperty("sendMsgPool"));
        GlobalParas.urlAndAddrPool = new ArrayBlockingQueue(cfg.getIntProperty("urlAndAddrPool"));

        GlobalParas.maxTry = cfg.getIntProperty("maxTry");
        GlobalParas.maxConnTimeLimit = cfg.getIntProperty("maxConnTimeLimit");
        GlobalParas.maxReadTimeLimit = cfg.getIntProperty("maxReadTimeLimit");
        GlobalParas.sendMq = cfg.getProperty("sendMq").split("[|]");

//        GlobalParas.sendGmsgMq = cfg.getProperty("sendGmsgMq");
//        GlobalParas.sendIvrmsgMq = cfg.getProperty("sendIvrmsgMq");
        GlobalParas.urlColumnName = cfg.getProperty("urlColumn");
        GlobalParas.pathColumnName = cfg.getProperty("pathColumn");
        GlobalParas.newColumn = cfg.getProperty("newColumn");
        GlobalParas.midColumnName = cfg.getProperty("midColumn");
        GlobalParas.httpUrl = cfg.getProperty("httpUrl");
        GlobalParas.rootPath = cfg.getProperty("rootPath");

        GlobalParas.myRequestPool = new ArrayBlockingQueue(cfg.getIntProperty("myRequestPool"));
        GlobalParas.send2MyHttpRequestPool = new ArrayBlockingQueue(cfg.getIntProperty("send2MyHttpRequestPool"));
        GlobalParas.httpPostAddr = cfg.getProperty("httpPostAddr").split("[;]");
        GlobalParas.send2RabbitmqPool = new ArrayBlockingQueue(cfg.getIntProperty("send2RabbitmqPool"));
        GlobalParas.httpTheadSize = cfg.getIntProperty("httpTheadSize");

        for (int i = 0; i < oriMsgMq.length; ++i) {
            String mq = oriMsgMq[i].split("[:]")[0];
            mpForMqName.put(mq, sendMq[i]);
        }

        for (int i = 0; i < oriMsgMq.length; ++i) {
            mpForSendPool.put(oriMsgMq[i].split("[:]")[0], new ArrayBlockingQueue(cfg.getIntProperty("sendMsgPool")));
        }
    }
}
